<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\i18n\Formatter;
use kartik\widgets\DatePicker;
/* @var $this yii\web\View */
/* @var $model frontend\models\PnHangHoa */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="pn-hang-hoa-form">
	<?php $form = ActiveForm::begin(['id'=>'pnKho']); ?>
	<div class="col-lg-12">
		<div class="row">
			<div class="col-lg-2">
			<?php if(!$model->isNewRecord):?>
				<div class="form-group field-pnhanghoa-so_ctu required has-success">
					<label class="control-label" for="pnhanghoa-so_ctu">Số CT</label>
					<input type="text" class="form-control" value="<?=$model->ma_ctu.'/'.$model->so_ctu?>" readonly>				
				</div>
			<?php endif;?>
			</div>			
			<div class="col-lg-3">
			<?php echo $form->field($model, 'ncc_id')->dropDownList($model->dsNCC,['prompt'=>'Chọn...',
				'onchange'	=>	"
					$.post('".Yii::$app->urlManager->createUrl('kho/ncc-info?id=')."'+$(this).val(), function(obj) {
						if(obj && obj.status == true){
							$('#".Html::getInputId($model, 'ten_ncc')."').val(obj.data.ten);
							$('#".Html::getInputId($model, 'mst_ncc')."').val(obj.data.mst);
						}
					});
            "
			]);?>
			</div>
			<div class="col-lg-3">
				<?= $form->field($model, 'ten_ncc')->textInput(['maxlength' => 100]) ?>
			</div>
			<div class="col-lg-2">
				<?= $form->field($model, 'mst_ncc')->textInput(['maxlength' => 14]) ?>
			</div>
			<div class="col-lg-2">
				<?#= $form->field($model, 'ky_hieu_hdon')->textInput(['maxlength' => 10]) ?>
			</div>
		</div>
		<div class="row">
			<div class="col-lg-2">
				<?= $form->field($model, 'ky_hieu_hdon')->textInput(['maxlength' => 10]) ?>
			</div>
			<div class="col-lg-2">
				<?= $form->field($model, 'so_hdon')->textInput(['maxlength' => 10]) ?>
			</div>
			<div class="col-lg-2">
			<?php 
				echo $form->field($model, 'ngay_hdon')->widget(DatePicker::classname(), [
					'options' => [
						'placeholder' => 'ngày/tháng/năm',
						'value' 	=> $model->ngay_hdon?date('d/m/Y',$model->ngay_hdon):date('d/m/Y'),
						// 'readonly'	=>	true
					],
					 'pluginOptions' => [
						'autoclose'=>true,
						'format' => 'dd/mm/yyyy',						
					 ]
				]);
			?>
			</div>
			<?/*
			<div class="col-lg-2">
				<?php echo $form->field($model, 'pptt_id')->dropDownList([
					'1'	=>	'Trên tiền hàng',
					'2'	=>	'Trên tiền thanh toán'
				]);?>
			</div> */?>
			<div class="col-lg-2">
				<?php echo $form->field($model, 'tk_thue')->dropDownList([
					'1131'	=>	'1131',
					'2231'	=>	'2231'
				]);?>
			</div>
		</div>
		<div class="row">
			<div class="col-lg-2">
				<?php echo $form->field($model, 'phong_ban_id')->dropDownList([
					'1'	=>	'Kho',
					'2'	=>	'Cửa hàng'
				]);?>
			</div>
			<div class="col-lg-2">
				<?php echo $form->field($model, 'kho_id')->dropDownList([
					'1'	=>	'Kho công ty',
					'2'	=>	'Kho chi nhánh 1'
				]);?>
			</div>
			<div class="col-lg-2">
				<?php echo $form->field($model, 'vi_tri_kho_id')->dropDownList([
					'1'	=>	'Lô 1',
					'2'	=>	'Lô 2'
				]);?>
			</div>
			<div class="col-lg-6">
				<?php echo $form->field($model, 'dien_giai')->textInput();?>
			</div>
		</div>
		<div class="row">
			<div class="col-lg-2">
			</div>
			<div class="col-lg-2">
			</div>
			<div class="col-lg-2">
			</div>
			<div class="col-lg-2">
				<?php echo $form->field($model, 'tien_hang')->textInput([
					'value' => number_format($model->tien_hang,0,',',' ')/*Yii::$app->format->asDecimal($model->tien_hang?$model->tien_hang:0,'vi-vn',[
						NumberFormatter::MIN_FRACTION_DIGITS => 0,
						NumberFormatter::MAX_FRACTION_DIGITS => 0,
					])*/,
					'class'	=>	'text-right form-control',
					'readonly'	=>	true]);?>
			</div>
			<div class="col-lg-2">
				<?php echo $form->field($model, 'tien_thue')->textInput([
					'value' => number_format($model->tien_thue,0,',',' ')/*Yii::$app->format->asDecimal($model->tien_thue?$model->tien_thue:0,'vi-vn',[
						NumberFormatter::MIN_FRACTION_DIGITS => 0,
						NumberFormatter::MAX_FRACTION_DIGITS => 0,
					])*/,
					'class'	=>	'text-right form-control',
					'readonly'	=>	true]);?>
			</div>
			<div class="col-lg-2">
				<?php echo $form->field($model, 'tien_thanh_toan')->textInput([
					'value' => number_format($model->tien_thanh_toan,0,',',' ')/*Yii::$app->format->asDecimal($model->tien_thanh_toan?$model->tien_thanh_toan:0,'vi-vn',[
						NumberFormatter::MIN_FRACTION_DIGITS => 0,
						NumberFormatter::MAX_FRACTION_DIGITS => 0,
					])*/,
					'class'	=>	'text-right form-control',
					'readonly'	=>	true]);?>
			</div>
		</div>
		<div class="row">
			<div class="col-lg-4 form-group">
				<?php echo Html::button('', ['class' => 'btn btn-primary glyphicon glyphicon-plus',
					'title'		=>	'Thêm dòng',
					'onclick'	=>	"
						$.post('".Yii::$app->urlManager->createUrl('kho/add-detail?type=input&row=')."'+$('#detailTable tbody tr').length, function(html) {
							$('#detailTable tbody').append(html);
						});
					"
				])?>
				<?php echo Html::button('', ['class' => 'btn btn-danger glyphicon glyphicon-minus', 'title' => 'Xóa dòng',
					'onclick'	=>	"
						jQuery('#detailTable .check:checked').each(function(i,o){
							if(jQuery('#detailTable tbody tr').length < 2)
								return;
							else
								jQuery(o).closest('tr').remove();
						});
					"
				])?>
			</div>
		</div>
		<div class="row">
			<div class="col-lg-12" style="overflow-x:scroll;">	
				<table id="detailTable" class="table table-striped table-bordered" style="width:130%;max-width:130%">
					<thead>
						<tr>
							<th width="20px">#</th>
							<th width="150px" class="text-center">Mã hàng</th>
							<th class="text-center">Tên hàng</th>
							<th width="80px" class="text-center">ĐVT</th>
							<th width="80px" title="Số lượng yêu cầu" class="text-center">SLYC</th>
							<th width="80px" class="text-center">SL thực</th>
							<th width="130px" class="text-center">Đơn giá</th>														
							<th width="140px" class="text-center">Tiền hàng</th>
							<th width="100px" class="text-center">% thuế</th>
							<th width="140px" class="text-center">Tiền thuế</th>
							<th width="120px" class="text-center">TK nợ</th>
							<th width="120px" class="text-center">TK có</th>
						</tr>
					</thead>
					<tbody>
					<?php $i = 0;
						$detail = $model->getDetails();						
						foreach($detail ? $detail : [new frontend\models\PnKhoDetail()] as $obj): $i++;
						?>	
						<tr>
							<td><input type="checkbox" class="check" style="margin-top:12px"></td>
							<td>
								<?php $name = "PnKhoDetail[$i][ma_hang]";?>
								<?=Html::dropDownList($name,$obj->ma_hang,$model->dmHangHoaByNhom,[
									'class'=>"ma-hang form-control",
									'onchange'	=>	"kho.nhap.hangHoaInfo('".Yii::$app->urlManager->createUrl('kho/hang-hoa-info?id=')."'+$(this).val(), this, event)",
								])?>
							</td>
							<td>
								<?php $field =	$form->field($obj, 'ten_hang')->label(false);
									$field->template = "{input}";
									echo $field->textInput(['class' => 'form-control ten-hang',
										'id'		=>	"PnKhoDetail$i-ten_hang",
										'name'	=> "PnKhoDetail[$i][ten_hang]"
									]);
								?>
							</td>
							<td>
								<?php $field =	$form->field($obj, 'dvt')->label(false);
									$field->template = "{input}";
									echo $field->textInput(['class' => 'dvt form-control',
										'id'		=>	"dvt$i",
										'name'	=> "PnKhoDetail[$i][dvt]"
									]);
								?>
							</td>
							<td>
								<?php $field =	$form->field($obj, 'sl_ycau')->label(false);
									$field->template = "{input}";
									echo $field->textInput(['class' => 'form-control text-right',
										'id'		=>	"sl_ycau$i",
										'name'	=> "PnKhoDetail[$i][sl_ycau]",
										'value'	=>	number_format($obj->sl_ycau,2,'.',','),
										// 'onfocus'=>	"yii.number_focus(this)",
										'onblur'	=>	"yii.float_number_blur(this)"
									]);
								?>
							</td>
							<td>
								<?php $field =	$form->field($obj, 'sl_thuc')->label(false);
									$field->template = "{input}";
									echo $field->textInput(['class' => 'sl-thuc form-control text-right',
										'id'		=>	"sl_thuc$i",
										'name'	=> "PnKhoDetail[$i][sl_thuc]",
										'value'	=>	number_format($obj->sl_thuc,2,'.',','),
										// 'onfocus'=>	"yii.number_focus(this)",
										'onblur'	=>	"yii.float_number_blur(this)",
										'onchange'	=>	"kho.nhap.tinhTien(this,event);"
									]);
								?>
							</td>
							<td>
								<?php $field =	$form->field($obj, 'don_gia')->label(false);
									$field->template = "{input}";
									echo $field->textInput(['class' => 'don-gia form-control text-right',
										'id'		=>	"don_gia$i",
										'name'	=> "PnKhoDetail[$i][don_gia]",
										'value'	=>	number_format($obj->don_gia,0,',',' '),
										'onfocus'=>	"yii.number_focus(this)",
										'onblur'	=>	"yii.number_blur(this)",
										'onchange'	=>	"kho.nhap.tinhTien(this,event);"
									]);
								?>
							</td>												
							<td>
								<?php $field =	$form->field($obj, 'thanh_tien')->label(false);
									$field->template = "{input}";
									echo $field->textInput(['class' => 'thanh-tien form-control text-right',
										'id'		=>	"thanh_tien$i",
										'name'	=> "PnKhoDetail[$i][thanh_tien]",
										'value'	=>	number_format($obj->thanh_tien,0,',',' '),
										'readonly'	=>	true
									]);
								?>
							</td>
							<td>
								<?=Html::dropDownList("PnKhoDetail[$i][thue_suat]",$obj->thue_suat,$model->dmThueSuat,[
									'class'=>"thue-suat form-control",
									'onchange'	=>	"kho.nhap.tinhTien(this,event);"
								])?>
							</td>	
							<td>
								<?php $field =	$form->field($obj, 'tien_thue')->label(false);
									$field->template = "{input}";
									echo $field->textInput(['class' => 'tien-thue form-control text-right',
										'id'		=>	"tien_thue$i",
										'name'	=> "PnKhoDetail[$i][tien_thue]",
										'value'	=>	number_format($obj->tien_thue,0,',',' '),
										'readonly'	=>	true
									]);
								?>
							</td>
							<td>
								<?php 
									$name = "PnKhoDetail[$i][tk_no]";?>
								<?=Html::dropDownList($name,$obj->tk_no,$model->tkNoList,['class'=>"tkNo form-control"])?>
							</td>
							<td>
								<?php 
									$name = "PnKhoDetail[$i][tk_co]";?>
								<?=Html::dropDownList($name,$obj->tk_co,$model->tkCoList,['class'=>"tkCo form-control"])?>
							</td>					
						</tr>
					<?php endforeach; ?>
					</tbody>
				</table>
			</div>	
		</div>		
	</div>
	<?php ActiveForm::end(); ?>
	<div class="form-group text-center">
		<?php echo Html::submitButton($model->isNewRecord ? 'Tạo mới' : 'Cập nhật', [
			'class' 		=> $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary',
			'onclick'	=>	"$('#pnKho').submit();"
		]) ?>
	</div>
</div>
	
